﻿using Gp.Scripts.Core;
using QFramework;
using UnityEngine;
using EventHandler = Gp.Scripts.Core.EventHandler;

namespace Gp.Scripts.GUI 
{
    /// <summary>
    /// 技能指示器显示参数，通过弱类型实现任意上下文参数传递
    /// </summary>
    public class ShowIndicatorArgs : WeakTypeObject
    {
        public ShowIndicatorArgs() { }

        public ShowIndicatorArgs(BaseUnit unit)
        {
            this["Unit"] = unit;
        }

        public BaseUnit Unit => Get<BaseUnit>("Unit");
        public SkillEntity SkillEntity => Get<SkillEntity>("SkillEntity");
    }    
    
    
    
    public abstract class BattleIndicatorBase
    {
        protected Transform Root { get; private set; }
        protected readonly EventHandler EventHandler = new();
        protected ShowIndicatorArgs Context { get; private set; }
        protected BattleUI3DNode UI3DNode { get; private set; }
        
        /// <summary>
        /// 仅在第一次载入时调用一次。 用于初始化UnityGameObject。
        /// </summary>
        public void Init(Transform root, BattleUI3DNode ui3DNode)
        {
            Root     = root;
            UI3DNode = ui3DNode;
            Root.OnUpdate(OnUpdate);
            OnInit();
        }

        
        protected virtual void OnInit() { }
        
        protected abstract void OnDisplay(ShowIndicatorArgs args);

        protected abstract void OnCleanUp();
        
        
        /// <summary>
        /// 每次技能选中时调用。
        /// </summary>
        public void Display(ShowIndicatorArgs args)
        {
            Root.gameObject.SetActive(true);
            EventHandler.UnRegisterAll();
            Context = args;
            OnDisplay(args);
        }

        public void CleanUp()
        {
            Root.gameObject.SetActive(false);
            EventHandler.UnRegisterAll();
            Context = null;
            OnCleanUp();
        }

        public virtual void OnUpdate() { }
        
        public void TryExecute(SkillEntity skill, SkillContext context)
        {
        }
    }


    
    // public class BattleIndicatorMono : MonoBehaviour
    // {
    //     public Action OnUpdate;
    //
    //     private void Update()
    //     {
    //         OnUpdate?.Invoke();
    //     }
    // }

}